package com.zx.zxapibackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zx.zxapibackend.model.domain.InterfaceCharging;
import com.zx.zxapibackend.service.InterfaceChargingService;
import com.zx.zxapibackend.mapper.InterfaceChargingMapper;

import com.zx.zxapicommon.common.BaseResponse;
import com.zx.zxapicommon.exception.BusinessException;
import com.zx.zxapicommon.model.enums.ErrorCode;
import com.zx.zxapicommon.model.vo.LockChargingVo;
import com.zx.zxapicommon.utils.ResultUtils;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Bro
 * @description 针对表【interface_charging】的数据库操作Service实现
 * @createDate 2023-07-18 11:03:05
 */
@Service
public class InterfaceChargingServiceImpl extends ServiceImpl<InterfaceChargingMapper, InterfaceCharging>
        implements InterfaceChargingService {


    /**
     * 获取当前最新剩余库存
     *
     * @param interfaceId
     * @return
     */
    @Override
    public BaseResponse getPresentAvailablePieces(Long interfaceId) {
        QueryWrapper<InterfaceCharging> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("interfaceId", interfaceId);
        InterfaceCharging interfaceCharging = this.getOne(queryWrapper);
        if (interfaceCharging == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        return ResultUtils.success(interfaceCharging.getAvailablePieces());
    }

    
    /**
     * 更新接口剩余可购买数量
     *
     * @param lockChargingVo
     * @return
     */
    @Override
    public BaseResponse updateAvailablePieces(LockChargingVo lockChargingVo) {
        Long interfaceId = lockChargingVo.getInterfaceId();
        Long orderNum = lockChargingVo.getOrderNum();
        UpdateWrapper<InterfaceCharging> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("interfaceId", interfaceId);
        updateWrapper.setSql("availablePieces = availablePieces -" + orderNum);
        boolean update = this.update(updateWrapper);
        if (update) {
            return ResultUtils.success(null);
        } else {
            return ResultUtils.error(ErrorCode.PARAMS_ERROR, "更新库存失败");
        }
    }

    /**
     * 解锁库存
     * @param lockChargingVo
     * @return
     */
    @Override
    @Transactional
    public BaseResponse unlockAvailablePieces(LockChargingVo lockChargingVo) {
        Long interfaceId = lockChargingVo.getInterfaceId();
        Long orderNum = lockChargingVo.getOrderNum();
        try {
            this.update(new UpdateWrapper<InterfaceCharging>().eq("interfaceId",interfaceId)
                    .setSql("availablePieces = availablePieces + "+orderNum));
        }catch (Exception e){
            return ResultUtils.error(ErrorCode.PARAMS_ERROR,"解锁库存失败");
        }
        return ResultUtils.success(null);
    }
}




